package com.ald.crossex.util;

import com.ald.crossex.dto.JqGridFilter;
import com.ald.crossex.dto.JqGridFilterRule;

/**
 * Created by lishijiong on 15/1/30.
 */
public class JqGridFilterUtils {
    /**
     * jqGrid 查询条件组装
     * @param filters
     * @return
     */
    public static String convertJqGridFilter2String(JqGridFilter filters) {

        String conditions = "and ( ";
        for (int i = 0; i < filters.getRules().size(); i++) {
            JqGridFilterRule rule = filters.getRules().get(i);
            if (i == 0) {
                conditions += convertJqGridFilterRuleOp2String(rule);
            } else {
                conditions += filters.getGroupOp() + " " + convertJqGridFilterRuleOp2String(rule);
            }

        }

        conditions += ") ";
        return conditions;
    }

    /**
     * jqGrid 查询条件拼接
     * @param rule
     * @return
     */
    private static String convertJqGridFilterRuleOp2String(JqGridFilterRule rule) {
        if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_BEGINS_WITH)) {
            return rule.getField() + " like '" + rule.getData() + "%' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_NOT_BEGINS_WITH)) {
            return rule.getField() + " not like '" + rule.getData() + "%' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_CONTAINS)) {
            return rule.getField() + " like '%" + rule.getData() + "%' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_NOT_CONTAIN)) {
            return rule.getField() + " not like '%" + rule.getData() + "%' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_ENDS_WITH)) {
            return rule.getField() + " like '%" + rule.getData() + "' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_NOT_ENDS_WITH)) {
            return rule.getField() + " not like '%" + rule.getData() + "' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_EQUAL)) {
            return rule.getField() + " = '" + rule.getData() + "' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_NOT_EQUAL)) {
            return rule.getField() + " <> '" + rule.getData() + "' ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_IN)) {
            String[] datas = rule.getData().split(",");
            String in = "";
            for(int i=0;i<datas.length;i++){
                if(i==0){
                    in = "'"+datas[i]+"'";
                }else{
                    in += ",'"+datas[i]+"'";
                }

            }
            return rule.getField() + " in (" + in + ") ";
        } else if (rule.getOp().equals(JqGridFilterRule.JQGRIDFILTERRULE_OP_NOT_IN)) {
            String[] datas = rule.getData().split(",");
            String in = "";
            for(int i=0;i<datas.length;i++){
                if(i==0){
                    in = "'"+datas[i]+"'";
                }else{
                    in += ",'"+datas[i]+"'";
                }

            }
            return rule.getField() + " not in (" + in + ") ";
        }
        return "";

    }

}
